package testClasses.misc;

import galaxies.GalaxyGenerator2;
import galaxies.GalaxySizeCalculator;
import gameAppLayer.GameSettings;

import java.awt.Polygon;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh.Mode;

import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.CoordConvert;
import utilities.CustomMesh;
import utilities.Key;
import view.EViewList;
import view.View;
import controller.AbstractController;
import controller.IController;

public class GalaxyGeneratorTest extends ATest {

	private static final Logger logger = Logger.getLogger(GalaxyGeneratorTest.class.getName());
	
	public GalaxyGeneratorTest(TestingSystem system) {
		super(system);
		name = "GalaxyGeneratorTest";
		logger.log(Level.INFO, "Created");
	}

	@Override
	public boolean execute() {
		logger.log(Level.INFO, "Execute()");
		
		HashMap<Key, IController> controllerMap = new HashMap<Key, IController>();
		eventManager.addControlMapReference(controllerMap);
		
		TestGameSettingsDefaults defaults = new TestGameSettingsDefaults(xmlData);
		GameSettings settings = defaults.settings;
		
		GalaxySizeCalculator galSizeCalc = new GalaxySizeCalculator();
		HashMap<String, ArrayList<ArrayList<Integer>>> galSizes = galSizeCalc.calcGalaxyDims(settings);
		HashMap<String, Double> galaxyRotations = galSizeCalc.rotations;
		
		
		
		
		
		GalaxyGenerator2 galGen = new GalaxyGenerator2();
		galGen.createGalaxies(settings, assetManager, eventManager, galSizes, galaxyRotations, xmlData);
		ArrayList<ArrayList<Integer>> locations = galGen.locations;
		
		ArrayList<Double> loc1;
		float x;
		float y;		
		HashMap<String, ArrayList<Integer>> offsets = galGen.offsets;
		
		//Display galaxy polygons
		HashMap<String, Polygon> polys = galGen.polys1;
		CustomMesh mesh2 = new CustomMesh(polys.size()*4);
		mesh2.setMode(Mode.Points);
		mesh2.setColor(ColorRGBA.Orange);
		mesh2.setPointSize(10);
		
		for (Map.Entry<String, Polygon> entry : polys.entrySet()) {
			Polygon poly = entry.getValue();
			String galRef = entry.getKey();
			
			ArrayList<Integer> off = offsets.get(galRef);			
			ArrayList<Double> cartOff = CoordConvert.hex2Cart(off);
			
			int[] xpoints = poly.xpoints;
			int[] ypoints = poly.ypoints;
			for (int i = 0; i < poly.npoints; i++) {
				//mesh2.addVertex((float) (xpoints[i]+cartOff.get(0)), (float) (ypoints[i]+cartOff.get(1)),0);
				mesh2.addVertex((float) (xpoints[i]), (float) (ypoints[i]),0);
			}			
		}
		
		mesh2.finish();
		mesh2.setStatic();
		
		Geometry geo2 = new Geometry("points2", mesh2);
		
		Material mat2 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat2.setBoolean("VertexColor", true);
		geo2.setMaterial(mat2);
		node.attachChild(geo2);
		
		
		
//		//Display galaxy bounds
//		//----------------------------------------------------
//		CustomMesh mesh1 = new CustomMesh(galSizes.size()*200);
//		mesh1.setMode(Mode.Points);
//		mesh1.setColor(ColorRGBA.Magenta);
//		mesh1.setPointSize(10);
//		
//		for (Map.Entry<String, ArrayList<ArrayList<Integer>>> entry : galSizes.entrySet()) {
//			ArrayList<ArrayList<Integer>> currGal = entry.getValue();
//			String ref = entry.getKey();
//			
//			ArrayList<Integer> off = offsets.get(ref);			
//			ArrayList<Double> cartOff = CoordConvert.hex2Cart(off);
//			
//			for (ArrayList<Integer> point: currGal) {
//				x = (float) (point.get(0) + cartOff.get(0));
//				y = (float) (point.get(1) + cartOff.get(1));
//				
//				mesh1.addVertex(x,y,0);
//			}
//		}
//		
//		mesh1.finish();
//		mesh1.setStatic();
//		
//		Geometry geo1 = new Geometry("points2", mesh1);
//		
//		Material mat1 = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat1.setBoolean("VertexColor", true);
//		geo1.setMaterial(mat1);
//		node.attachChild(geo1);
		
		//----------------------------------------------------
		
		
		
		//Display star locations
		//----------------------------------------------------
		
		CustomMesh mesh3 = new CustomMesh(locations.size());
		//CustomMesh mesh4 = new CustomMesh(haloLocations.size());
		mesh3.setMode(Mode.Points);
		//mesh4.setMode(Mode.Points);
		mesh3.setColor(ColorRGBA.Blue);
		//mesh4.setColor(ColorRGBA.Orange);
		mesh3.setPointSize(5);		
		//mesh4.setPointSize(5);
		
		for (ArrayList<Integer> location : locations) {
			
			loc1 = CoordConvert.hex2Cart(location);
			x = loc1.get(0).floatValue();
			y = loc1.get(1).floatValue();
			
			mesh3.addVertex(x, y, 0.2f);			
		}
		
//		for (ArrayList<Integer> location: haloLocations) {
//			loc1 = CoordConvert.hex2Cart(location);
//			x = loc1.get(0).floatValue();
//			y = loc1.get(1).floatValue();
//			
//			mesh4.addVertex(x, y, 0.5f);			
//		}
				
		mesh3.finish();
//		mesh4.finish();
		mesh3.setStatic();
//		mesh4.setStatic();
//		
		Geometry geo3 = new Geometry("points1", mesh3);
//		Geometry geo4 = new Geometry("points2", mesh4);
		Material mat3 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
//		Material mat4 = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
		mat3.setBoolean("VertexColor", true);
//		mat4.setBoolean("VertexColor", true);
		//mat.setColor("Color", ColorRGBA.Green);
		geo3.setMaterial(mat3);
//		geo4.setMaterial(mat4);
		node.attachChild(geo3);
//		node.attachChild(geo4);
		
		
		

		
		return false;
	}

}
